package com.innovation.chengzhangriji.ui.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.StateSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.CuteImageView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DiaryAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public DiaryAdapter2(DiaryAdapterAgent agent) {
        this.agent = agent;
    }


    public static interface DiaryAdapterAgent extends OnItemClickListener {
        /**
         * 宝宝头像
         */
        public String getBabyHeadUrl(int position);

        /**
         * 日记本名称
         */
        public String getDiaryName(int position);

        /**
         * 宝宝性别
         */
        public int getBabySex(int position);

        /**
         * 修改日记本名称
         */
        // public void alterDiaryName(int position, String name);

        /**
         * 日记本的数量
         */
        public int getDiaryCount();

        /**
         * 项的点击事件
         */
        @Override
        public void OnItemClick(int position);
    }

    private int TYPE_NORMAL = 0;
    private int TYPE_LINE = 1;

    private DiaryAdapterAgent agent;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_LINE) {
            return new LineHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.include_line, null));
        }
        return new DiaryViewHolder2(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_diary_2, null), agent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == 0 || position == getItemCount() - 1) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
            if(layoutParams == null) {
                layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            }
            if(position == 0) {
                layoutParams.setMargins(0, (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, holder.itemView.getResources().getDisplayMetrics())), 0, 0);
            } else {
                layoutParams.setMargins(0, 0, 0, 0);
            }
            holder.itemView.setLayoutParams(layoutParams);
            if(getItemCount() == 0) {
                holder.itemView.setVisibility(View.GONE);
            } else {
                holder.itemView.setVisibility(View.VISIBLE);
            }
            return;
        }
        position -= 1;
//        ((DiaryViewHolder2)holder).imageSex.setVisibility(View.VISIBLE);
//        if(agent.getBabySex(position) == 1) {
//            ((DiaryViewHolder2)holder).imageSex.setImageResource(R.mipmap.icon_nan);
//        } else if(agent.getBabySex(position) == 2) {
//            ((DiaryViewHolder2)holder).imageSex.setImageResource(R.mipmap.icon_nv);
//        } else {
//            ((DiaryViewHolder2)holder).imageSex.setVisibility(View.GONE);
//        }
        ((DiaryViewHolder2)holder).roundedHeadView.setAvatar(agent.getBabyHeadUrl(position));
        ((DiaryViewHolder2)holder).roundedHeadView.setSex(agent.getBabySex(position));
        ((DiaryViewHolder2)holder).txtDiaryName.setText(agent.getDiaryName(position));
//        ImageLoader.getInstance().displayImage(agent.getBabyHeadUrl(position), ((DiaryViewHolder2)holder).roundedHeadView, Config.getOptionsHeadUnKnown());
        if(position == getItemCount() - 3) {
            ((DiaryViewHolder2)holder).lineView.setVisibility(View.INVISIBLE);
//            ((RelativeLayout.LayoutParams)holder.lineView.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        } else {
            ((DiaryViewHolder2)holder).lineView.setVisibility(View.VISIBLE);
//            ((RelativeLayout.LayoutParams)holder.lineView.getLayoutParams()).addRule(RelativeLayout.ALIGN_LEFT, R.id.text_diary_name);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position > 0 && position < getItemCount() - 1) {
            return TYPE_NORMAL;
        }
        return TYPE_LINE;
    }

    @Override
    public int getItemCount() {
        return agent.getDiaryCount();
    }

}
